import LoginService from './services/loginService.js';
import Vue from 'vue';
import {
    serverPath, json
} from '../common/config.js';
import {
    msg
} from '../common/utils.js';
import store from 'store';

let loginService = new LoginService();
var vm = new Vue({
    el: '#loginController',
    data: {
        user: {
            username: '',
            password: '',
        }
    },
    methods: {
        login: async function () {
            if (!this.user.username || 0 == this.user.username.length)
                await msg('请输入用户名');
            if (!this.user.password || 0 == this.user.password.length)
                await msg('请输入密码');
            let res = await loginService.loginAsync(this.user.username, this.user.password);

            if (!res.type) {
                for (var item in res) {
                    msg(res[item][0]);
                    return;
                }
            }
            else if (json === res.type) {
                var response = res.body;
                if ('Success' === response.result) {
                    store.set("AuthCode", response.data);
                    window.location.href="/#/home";
                }
                else {
                    await msg(response.message);
                }
            }
        }
    }
});
